<template>
  <div id="pubNoticeTopSearch">
    <div class="search-box">
      <el-form
        ref="ruleForm"
        :inline="true"
        :rules="rules"
        :model="formInline"
        class="demo-form-inline"
      >
        <el-form-item prop="timeType">
          <el-select v-model="formInline.timeType" placeholder="创建时间">
            <el-option
              v-for="item in timeTypeList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="createTime">
          <el-date-picker
            v-model="formInline.createTime"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item prop="searchContent">
          <el-input
            clearable
            v-model="formInline.searchContent"
            placeholder="标题、ID"
            size="medium"
            class="numberInput"
          ></el-input>
        </el-form-item>
        <el-form-item label="通知状态" prop="pubState">
          <el-select v-model="formInline.pubState" clearable placeholder="全部">
            <el-option
              v-for="item in pubStateList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二审状态" prop="twoAuditingState">
          <el-select
            v-model="formInline.twoAuditingState"
            clearable
            placeholder="全部"
          >
            <el-option
              v-for="item in twoAuditingStateList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="触达通道" prop="touchType">
          <el-select
            clearable
            v-model="formInline.touchType"
            placeholder="请选择"
            class="activeType"
          >
            <el-option
              v-for="item in touchTypeList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="s-btn">
          <el-button
            type="success"
            icon="el-icon-search"
            @click="toSearch('ruleForm')"
          >
            搜索
          </el-button>
          <el-button type="success" plain @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { sysNormalDisable } from '@utils/dictionariesFunc'
export default {
  props: {
    formInline: {
      type: Object,
      default: function() {
        return {
          timeType: '',
          createTime: [],
          searchContent: '',
          pubState: '', // 通知状态
          twoAuditingState: '', // 二审状态,默认全部
          touchType: '', // 触达通道,默认全部
          startTime: '', // 开始时间
          endTime: '' // 结束时间
        }
      }
    }
  },
  data() {
    return {
      timeTypeList: [
        { dictLabel: '创建时间', dictValue: 'CREATEDTIME' },
        { dictLabel: '发布时间', dictValue: 'PUBTIME' }
      ],
      // 通知状态列表
      pubStateList: [
        { dictLabel: '全部', dictValue: '' },
        { dictLabel: '已发布', dictValue: 'PUBLISH' },
        { dictLabel: '未发布', dictValue: 'UNPUBLISHED' }
      ],
      // 二审状态列表
      twoAuditingStateList: [{ dictLabel: '全部', dictValue: '' }],
      // 触达通道列表
      touchTypeList: [{ dictLabel: '全部', dictValue: '' }],
      rules: {
        timeType: {},
        searchContent: {},
        twoAuditingState: {},
        pubState: {},
        touchType: {},
        createTime: { type: 'array' }
      }
    }
  },
  watch: {
    'formInline.createTime': {
      handler(newVal, oldVal) {
        if (newVal && newVal.length > 1) {
          this.formInline.startTime = newVal[0]
          this.formInline.endTime = newVal[1]
        } else {
          this.formInline.startTime = ''
          this.formInline.endTime = ''
        }
      },
      deep: true
    }
  },
  mounted() {
    this.getTopSearch()
  },
  methods: {
    // 获取顶部搜索条件
    async getTopSearch() {
      // 获取触达通道
      const touchTypeList = await sysNormalDisable(
        'activity_notice_type',
        'touchType'
      )
      this.touchTypeList = this.touchTypeList.concat(touchTypeList)
      // 获取审核状态
      const auditingStateList = await sysNormalDisable('activity_check_statu')
      this.twoAuditingStateList = this.twoAuditingStateList.concat(
        auditingStateList
      )
      this.setDefaultValue()
      //触发列表获取权限
      this.$emit('toSearch')
    },
    /*设置默认数据*/
    setDefaultValue() {
      this.formInline.timeType = this.timeTypeList[0].dictValue
    },
    // 搜索
    toSearch(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('toSearch')
        } else {
          return false
        }
      })
    },
    // 重置
    reset() {
      this.$refs['ruleForm'].resetFields()
      //设置默认数据
      this.setDefaultValue()
    }
  }
}
</script>
<style lang="less">
#pubNoticeTopSearch {
  .el-input {
    &.el-input--suffix {
      width: 182px !important;
    }
  }
}
</style>
